knitr::opts_chunk$set(echo = TRUE)
First, start with installing the relevant packages.
## Indlæser krævet pakke: ggplot2
## Indlæser krævet pakke: lubridate
##
## Vedhæfter pakke: 'lubridate'
## De følgende objekter er maskerede fra 'package:base':
##
## date, intersect, setdiff, union
## Indlæser krævet pakke: PerformanceAnalytics
## Indlæser krævet pakke: xts
## Indlæser krævet pakke: zoo
##
## Vedhæfter pakke: 'zoo'
## De følgende objekter er maskerede fra 'package:base':
##
## as.Date, as.Date.numeric
##
## Vedhæfter pakke: 'PerformanceAnalytics'
## Det følgende objekt er maskeret fra 'package:graphics':
##
## legend
## Indlæser krævet pakke: quantmod
## Indlæser krævet pakke: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ══ Need to Learn tidyquant? ════════════════════════════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
##
## Vedhæfter pakke: 'dplyr'
## De følgende objekter er maskerede fra 'package:xts':
##
## first, last
## De følgende objekter er maskerede fra 'package:stats':
##
## filter, lag
## De følgende objekter er maskerede fra 'package:base':
##
## intersect, setdiff, setequal, union
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.4 ✓ stringr 1.4.0
## ✓ tidyr 1.1.3 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
Here we read in the data set and put it in a data frame.
df <- read_csv("Africa_19_20.csv")
## Rows: 90707 Columns: 31
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): event_id_cnty, event_date, event_type, sub_event_type, actor1, ass...
## dbl (13): data_id, iso, event_id_no_cnty, year, time_precision, inter1, inte...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
df2 <- read_xls("Scrape-Africa.xls")
There are some irrelevant columns that we want to remove. Then we save the cleaned data in a new data frame. By doing this we are not tampering with the original data for transparency.
clean <- c("iso", "iso3", "event_id_no_city", "event_id_no_cnty", "event_id_cnty", "time_precision", "inter1", "inter2", "interaction", "geo_precision")
cleandata <- df[,!(names(df) %in% clean)]
We want to have the date in our data frame listed as a date:
cleandata <- cleandata %>%
mutate(event_date = dmy(event_date))
We don’t want to include the year 2018:
cleandata <- cleandata %>%
filter(year > 2018)
Now that the data is clean, we can start using it. Firstly, we want to get an overview of the events in Africa, we want to make an interactive map showing the events geographically.
Here we install the neccesary package “leaflet”.
#install.packages("leaflet")
library(leaflet)
##
## Vedhæfter pakke: 'leaflet'
## Det følgende objekt er maskeret fra 'package:xts':
##
## addLegend
Here we specify which parameters to include when clicking in a specific event.
cleandata %>%
mutate(content = paste0('<b>Event type:</b> ', event_type, '<br>', '<b>Fatalities: </b>', fatalities, '<br>', '<b>Event date:</b> ', event_type, '<br>', '<b>Country:</b> ', country, '<br>')) -> cleandata
Here we specify how the map will take the latitude and longitude of the data set and make clusters if the events are close to each other, depending on how much you zoom in/out.
cleandata %>%
leaflet() %>%
addTiles() %>%
addMarkers(~longitude, ~latitude, label = ~event_type, popup = ~content, clusterOptions = markerClusterOptions())